package com.doyens.controller;

import com.doyens.common.R;
import com.doyens.entity.Teacher;
import com.doyens.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/teacher")
@Api(tags = "讲师管理接口")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @GetMapping("/list")
    @ApiOperation(value = "下拉框展示讲师姓名列表")
    public R<List<Teacher>> nameList(){
        List<Teacher> list = teacherService.list();
        return R.success(list);
    }

    @PostMapping
    @ApiOperation(value = "保存讲师方法")
    public R<String> save(Teacher teacher) {
        //接受的添加菜品的所有字段
        log.info(teacher.toString());
        boolean save = teacherService.save(teacher);
        if (save){
            return R.success("添加讲师成功...");
        }
        return R.error("添加讲师失败");
    }

    @DeleteMapping
    @ApiOperation(value = "删除讲师方法")
    public R<String> delete(Integer id) {
        log.info("删除的讲师id为:{}", id);
        //执行删除
        boolean remove = teacherService.removeById(id);
        if (remove){
            return R.success("删除讲师成功");
        }
        return R.error("删除讲师失败...");
    }

    @PutMapping
    @ApiOperation(value = "修改讲师信息方法")
    public R<String> update(Teacher teacher){
        log.info("修改后的讲师信息:{}",teacher.toString());
        boolean update = teacherService.updateById(teacher);
        if (update){
            return R.success("修改讲师信息成功...");
        }
        return R.error("修改讲师信息失败...");
    }
}
